#include <clock.h>
#include <cmsis_os.h>
#include <bsp.h>

namespace os {
void sleep(units::time::millisecond_t const duration) {
    if (duration.to<float>() < 0) {
        return;
    }
    if (duration.value() > 0.98F) {
        osDelay(std::max<uint32_t>(duration.to<uint32_t>(), 1U));
        return;
    }
    bsp::delay_us((units::time::microsecond_t(duration)).to<uint32_t>());
}

units::time::millisecond_t getTime() { return units::time::millisecond_t{static_cast<float>(xTaskGetTickCount())}; }

units::time::millisecond_t getOsTime() { return getTime(); }

#if defined(BSP_HAS_DWT)
units::time::microsecond_t getDWTTime_u32() { return units::time::microsecond_t{bsp::getGlobalTime_us_fp32()}; }
uint64_t getDWTTime_i64() { return bsp::getGlobalTime_us_i64(); }
#endif
}  // namespace os